package com.chap18;

import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/**
 * <p>
 * <code>LockEscalationDemo</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-05-17 16:29
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
@Slf4j
public class LockEscalationDemo9_1 {
    public static void main(String[] args) throws InterruptedException {

       log.debug(ClassLayout.parseInstance(new Object()).toPrintable());//001 无锁

        Thread.sleep(4000);

        Object obj = new Object();
        //这里obj是匿名偏向状态
                    obj.wait(10);
        log.debug(Thread.currentThread().getName()+"wait后执行中。。。\n"+ClassLayout.parseInstance(obj).toPrintable());//10 重量级锁
        new Thread(()->{
            log.debug(Thread.currentThread().getName()+"开始执行。。。\n"+ClassLayout.parseInstance(obj).toPrintable());//101 这里obj是匿名偏向状态
            synchronized (obj){
                log.debug(Thread.currentThread().getName()+"获取锁执行中。。。\n"+ClassLayout.parseInstance(obj).toPrintable());//10 重量级锁

            }
                log.debug(Thread.currentThread().getName()+"释放锁。。。\n"+ClassLayout.parseInstance(obj).toPrintable());//10 重量级锁  因为要清除monitor对象 暂时看到还是重量级锁


        },"thread1").start();

        Thread.sleep(5000);
        log.debug(Thread.currentThread().getName()+"释放锁。。。\n"+ClassLayout.parseInstance(obj).toPrintable());//001 无锁
     }
}
